package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<User> findAll() {
        return userMapper.selectList(null);
    }

    @Override
    public User getUserBy(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public List<User> getUserByNS(User user) {
        //根据对象不为null的属性充当where条件
        QueryWrapper<User> queryWrapper =new QueryWrapper(user);

        queryWrapper.eq("name",user.getName())
                    .eq("sex", user.getSex());
        return  userMapper.selectList(queryWrapper);

    }


    @Override
    public List<User> getByName(String name) {
        QueryWrapper queryWrapper =new QueryWrapper();
        queryWrapper.like("name", name);
        return userMapper.selectList(queryWrapper);
    }

    @Override
    public void deleteUserByName(User user) {
        userMapper.delete(new QueryWrapper<>(user));
    }

    @Override
    public void deleteUser(User user) {
//        QueryWrapper<User> queryWrapper =new QueryWrapper(user);
//
//        queryWrapper.eq("name",user.getName())
//                .eq("sex", user.getSex());
//        userMapper.delete(queryWrapper);
        userMapper.delete(new QueryWrapper<>(user));
    }

    @Override
    public void deleteUserById(Integer id) {
        userMapper.deleteById(id);
    }

    @Override
    public void SaveUser(User user) {
        userMapper.insert(user);
    }


    @Override
    public List<User> getUserBySex(User user) {
        QueryWrapper<User> queryWrapper =new QueryWrapper(user);
        queryWrapper.like("name", user.getName())
                    .eq("sex", user.getSex());

        return userMapper.selectList(queryWrapper);
    }

    @Override
    public void updateUser(User user) {
        //根据对象中不为null的属性当set条件 id当作where条件
        userMapper.updateById(user);
    }


}
